package com.shop.shoporder.service.impl;


import com.shop.shoporder.entity.Groupbuy;
import com.shop.shoporder.entity.GroupbuyInfo;
import com.shop.shoporder.entity.GroupbuyParticipate;
import com.shop.shoporder.mapper.GroupbuyMapper;
import com.shop.shoporder.mapper.GroupbuyParticipateMapper;
import com.shop.shoporder.service.GroupbuyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("groupbuyService")
public class GroupbuyServiceImpl implements GroupbuyService {

    @Autowired
    GroupbuyMapper groupbuyMapper;
    @Autowired
    GroupbuyParticipateMapper groupbuyParticipateMapper;

    Logger logger= LoggerFactory.getLogger(GroupbuyServiceImpl.class);
    @Override
    public int deleteByPrimaryKey(int groupbuyId) {
        return groupbuyMapper.deleteByPrimaryKey(groupbuyId);
    }

    @Override
    public int insert(Groupbuy record) {
        return groupbuyMapper.insert(record);
    }

    @Override
    @Transactional
    public int insertSelective(Groupbuy record) {
        return groupbuyMapper.insertSelective(record);
    }

    @Override
    public Groupbuy selectByPrimaryKey(int groupbuyId) {
        return groupbuyMapper.selectByPrimaryKey(groupbuyId);
    }

    @Override
    public int updateByPrimaryKeySelective(Groupbuy record) {
        return groupbuyMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByPrimaryKey(Groupbuy record) {
        return groupbuyMapper.updateByPrimaryKey(record);
    }

    @Override
    public Groupbuy getGroupbuyByUserNoId(int userNoId) {
        return groupbuyMapper.getGroupbuyByUserNoId(userNoId);
    }

    @Override
    public List<GroupbuyInfo> getGroupbuyListByGroupbuyGoodId(int groupbuygoodId) {
        return groupbuyMapper.getGroupbuyListByGroupbuyGoodId(groupbuygoodId);
    }

    @Override
    public List<Groupbuy> getGroupbuyCount(int groupbuygoodId) {
        return groupbuyMapper.getGroupbuyCount(groupbuygoodId);
    }

    @Override
    public List<GroupbuyInfo> getGroupbuyListByUserNoId(int userNoId) {
        return groupbuyMapper.getGroupbuyListByUserNoId(userNoId);
    }

    @Override
    public Groupbuy getGroupbuyByUserNoId(int userNoId, int groupbuygoodId) {
        return groupbuyMapper.groupbuyFail(userNoId, groupbuygoodId);
    }

    @Override
    public List<GroupbuyInfo> getGroupbuyAndParticpaterInfo(int groupbuyId) {
        return groupbuyMapper.getGroupbuyAndParticpaterInfo(groupbuyId);
    }

    @Override
    public Map<String, Object> updateGroupbuyStatus(int groupbuyId, int participateId) {
        System.out.println("---修改团购状态-->"+groupbuyId+"------>"+participateId);
        Map<String, Object> map = new HashMap<>();
        if (groupbuyId != 0&&participateId==0) {
            Groupbuy groupbuy = new Groupbuy();
            //   groupbuy.setGroupbuyState("1");//0未支付，1已支付
            groupbuy.setInitiateState("1");
            groupbuy.setGroupbuyId(groupbuyId);
            //修改
            int row = 0;
            row = groupbuyMapper.updateByPrimaryKeySelective(groupbuy);
            if (row > 0) {
                map.put("groupbuy",groupbuy);
                System.out.println("---------------团购状态更新结束---------------->");
                return map;
            } else {
                return null;
            }

        } else if (groupbuyId!=0&&participateId != 0) {  //（2）、参与者修改
            System.out.println("---------------参与团购状态更新---------------->"+participateId);
            GroupbuyParticipate groupbuyParticipate = new GroupbuyParticipate();
            groupbuyParticipate.setParticipateState("1");
            groupbuyParticipate.setParticipateId(participateId);
            //修改
            int row1 = 0;
            row1 = groupbuyParticipateMapper.updateByPrimaryKeySelective(groupbuyParticipate);
            if (row1 > 0) {
                map.put("groupbuyParticipate",groupbuyParticipate);
                System.out.println("---------------团购状态更新结束---------------->");
                return map;
            } else {
                return null;
            }
        } else {
            //两个参数都为空
            return null;
        }

    }
}
